package com.fand.common.interceptor;

import java.util.Map;
import java.util.Map.Entry;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.jasig.cas.client.authentication.AttributePrincipal;
import org.springframework.util.CollectionUtils;
import org.springframework.web.servlet.AsyncHandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.fand.common.service.CommonService;
import com.fand.context.ContextHolder;
import com.fand.router.support.RouterKeySupport;
import com.fand.site.dto.SiteDto;

public class FanDHandlerInterceptor extends HandlerInterceptorAdapter implements AsyncHandlerInterceptor {

	private CommonService commonService;

	private RouterKeySupport routerKeySupport;

	@Override
	public void afterCompletion(final HttpServletRequest request, final HttpServletResponse response, final Object handler,
			final Exception ex) throws Exception {
	}

	@Override
	public void afterConcurrentHandlingStarted(final HttpServletRequest request, final HttpServletResponse response,
			final Object handler) throws Exception {
	}

	@Override
	public void postHandle(final HttpServletRequest request, final HttpServletResponse response, final Object handler,
			final ModelAndView modelAndView) throws Exception {
		if (modelAndView != null) {
			String viewName = modelAndView.getViewName();

			if (StringUtils.isNotBlank(viewName)) {
				viewName = "default/" + viewName;

				modelAndView.setViewName(viewName);
			}
		}
	}

	@Override
	public boolean preHandle(final HttpServletRequest request, final HttpServletResponse response, final Object handler)
			throws Exception {
		ContextHolder.setId(request.getSession().getId());

		final String servletPath = request.getServletPath();

		if (StringUtils.startsWith(servletPath, "/resources")) {
			return true;
		}

		final AttributePrincipal principal = (AttributePrincipal) request.getUserPrincipal();

		if (principal != null) {
			response.sendRedirect(request.getContextPath() + "/error/404");

			final String userName = principal.getName();

			if (StringUtils.isNotBlank(userName)) {
				ContextHolder.setId(userName);
			}
		}

		this.routerKeySupport.setGroupId(ContextHolder.getId(), "dev");

		final String domainUrl = request.getServerName();

		SiteDto site = this.commonService.getSiteService().selectSiteBy2ForMappedObject(domainUrl);

		if (site == null) {
			site = this.commonService.getSiteService().selectSiteByDefaultForMappedObject();
		}

		request.setAttribute("fandSite", site);

		final Map<String, String> variableMap = this.commonService.getConfigurationService().getVariableService()
				.saveVariableBy1ForMap();

		if (!CollectionUtils.isEmpty(variableMap)) {
			for (final Entry<String, String> variableEntry : variableMap.entrySet()) {
				request.setAttribute(variableEntry.getKey(), variableEntry.getValue());
			}
		}

		request.setAttribute("fandServletPath", servletPath);

		return true;
	}

	public void setCommonService(final CommonService commonService) {
		this.commonService = commonService;
	}

	public void setRouterKeySupport(final RouterKeySupport routerKeySupport) {
		this.routerKeySupport = routerKeySupport;
	}
}
